//01:
import { legacy_createStore as createStore } from 'redux';
import { stateType } from '../types/types'
//02: 定义初始的state
const defaultState = {
    count: 100,
    user: {
        name: "徐祯卿",
        age: 30
    }
}

// interface stateType {
//     count: number,
//     user: {
//         name: string,
//         age: number
//     }
// }

type actionType = {
    type: string,
    payload?: any
}

// 03: 定义reducer
const reducer = (state: stateType = defaultState, action: actionType) => {
    if (action.type == '+') {
        // state.count += 1
        // console.log(state);

        return {
            ...state,
            count: action.payload ? state.count + action.payload : state.count + 1,
        }
    }
    if (action.type == 'add') {
        // state.count += 1
        // console.log(state);

        return {
            ...state,
            user: {
                ...state.user,
                age: state.user.age + 5
            }
        }
    }

    return state
}


// 04: 创建仓库
// 第一种方式: 声明windiow 类型
// declare let window: any;

//第二种方式:  
// @ts-ignore  表示取消当前行的类型校验
const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())


export default store